package com.customization.unionpay.market.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.MarketPersonalTaxWorkflowService;
import com.customization.unionpay.market.util.FormUtil;
import com.customization.unionpay.market.util.WorkflowUtil;
import weaver.backup.logging.Logger;
import weaver.backup.logging.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.workflow.webservices.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 营销个税计提单业务实现类
 */
public class MarketPersonalTaxWorkflowServiceImpl implements MarketPersonalTaxWorkflowService {

    public static final Logger log = LoggerFactory.getLogger(MarkertingDailyServiceImpl.class);


    @Override
    public String createMarketPersonalTaxWorkflow(User user, JSONObject checkReimbursemengInfo) {
        WorkflowService workflowService =  new WorkflowServiceImpl();
        WorkflowRequestInfo requestInfo = new WorkflowRequestInfo();

        List<WorkflowRequestTableField> tableFields = new ArrayList<>();
        //表单号 流程唯一标识，流程发起后自动生成
        WorkflowRequestTableField bdh = new WorkflowRequestTableField();
        bdh.setFieldName("bdh");
        bdh.setFieldValue(UUID.randomUUID().toString());
        tableFields.add(bdh);
        //业务类型 默认为营销个账提计
        WorkflowRequestTableField ywlx = new WorkflowRequestTableField();
        ywlx.setFieldName("ywlx");
        ywlx.setFieldValue("0");
        tableFields.add(ywlx);
        //申请人 自动取值当前报销人
        WorkflowRequestTableField sqr = new WorkflowRequestTableField();
        sqr.setFieldName("sqr");
        sqr.setFieldValue(user.getUID()+"");
        tableFields.add(sqr);
        //报账公司 自动取值当前报销人分部
        WorkflowRequestTableField bzgs = new WorkflowRequestTableField();
        bzgs.setFieldName("bzgs");
        bzgs.setFieldValue(user.getUserSubCompany1()+"");
        tableFields.add(bzgs);
        //报账部门 自动取值当前报销人的部门
        WorkflowRequestTableField bzbm = new WorkflowRequestTableField();
        bzbm.setFieldName("bzbm");
        bzbm.setFieldValue(user.getUserDepartment()+"");
        tableFields.add(bzbm);
        //业务日期 自动取当前日期
        String nowDate = DateUtil.formatDate(new Date());
        WorkflowRequestTableField ywrq = new WorkflowRequestTableField();
        ywrq.setFieldName("ywrq");
        ywrq.setFieldValue(nowDate);
        tableFields.add(ywrq);
        //入账日期 自动取当前日期
        WorkflowRequestTableField rzrq = new WorkflowRequestTableField();
        rzrq.setFieldName("rzrq");
        rzrq.setFieldValue(nowDate);
        tableFields.add(rzrq);
        //预算控制期 自动取当前日期
        WorkflowRequestTableField yskzq = new WorkflowRequestTableField();
        yskzq.setFieldName("yskzq");
        yskzq.setFieldValue(nowDate);
        tableFields.add(yskzq);
        //营销事项申请单号
        WorkflowRequestTableField yxsxsqdh = new WorkflowRequestTableField();
        yxsxsqdh.setFieldName("yxsxsqdh");
        yxsxsqdh.setFieldValue(checkReimbursemengInfo.getString("bdh"));
        tableFields.add(yxsxsqdh);
        //项目名称
        WorkflowRequestTableField xmmc = new WorkflowRequestTableField();
        xmmc.setFieldName("xmmc");
        xmmc.setFieldValue(checkReimbursemengInfo.getString("xmmc"));
        tableFields.add(xmmc);
        //项目编号
        WorkflowRequestTableField xmbh = new WorkflowRequestTableField();
        xmbh.setFieldValue("xmbh");
        xmbh.setFieldValue(checkReimbursemengInfo.getString("xmbh"));
        tableFields.add(xmbh);
        //项目维度指认
        WorkflowRequestTableField xmwdzr = new WorkflowRequestTableField();
        xmwdzr.setFieldName("xmwdzr");
        xmwdzr.setFieldValue(checkReimbursemengInfo.getString("xmwdzr"));
        tableFields.add(xmwdzr);
        //重点工作编码
        WorkflowRequestTableField zdgzbm = new WorkflowRequestTableField();
        zdgzbm.setFieldName("zdgzbm");
        zdgzbm.setFieldValue(checkReimbursemengInfo.getString("zdgzbm"));
        tableFields.add(zdgzbm);
        //重点工作名称
        WorkflowRequestTableField zdgzmc = new WorkflowRequestTableField();
        zdgzmc.setFieldName("zdgzmc");
        zdgzmc.setFieldValue(checkReimbursemengInfo.getString("zdgzmc"));
        tableFields.add(zdgzmc);
        //供应商编号
        WorkflowRequestTableField gysbh = new WorkflowRequestTableField();
        gysbh.setFieldName("gysbh");
        gysbh.setFieldValue(checkReimbursemengInfo.getString("gysbh"));
        tableFields.add(gysbh);
        //供应商名称
        WorkflowRequestTableField gysmc = new WorkflowRequestTableField();
        gysmc.setFieldName("gysmc");
        gysmc.setFieldValue(checkReimbursemengInfo.getString("gysmc"));
        tableFields.add(gysmc);
        //供应商地点
        WorkflowRequestTableField gysdd = new WorkflowRequestTableField();
        gysdd.setFieldName("gysdd");
        gysdd.setFieldValue(checkReimbursemengInfo.getString("gysdd"));
        tableFields.add(gysdd);
        //营销平台活动授权码
        WorkflowRequestTableField yxpthdsqm = new WorkflowRequestTableField();
        yxpthdsqm.setFieldName("yxpthdsqm");
        yxpthdsqm.setFieldValue(checkReimbursemengInfo.getString("yxpthdm"));
        tableFields.add(yxpthdsqm);

        tableFields.forEach(o->{
            o.setEdit(true);
            o.setView(true);
        });


        WorkflowRequestTableRecord[] mainTableRecords = new WorkflowRequestTableRecord[1];
        WorkflowRequestTableRecord tableRecord = new WorkflowRequestTableRecord();
        tableRecord.setWorkflowRequestTableFields(tableFields.toArray(new WorkflowRequestTableField[tableFields.size()]));
        mainTableRecords[0] = tableRecord;

        WorkflowMainTableInfo mainTableInfo = new WorkflowMainTableInfo();
        mainTableInfo.setRequestRecords(mainTableRecords);
        //添加主字段数据
        requestInfo.setWorkflowMainTableInfo(mainTableInfo);

        //添加明细表数据
        //经济事项 存在与核销对账单的明细表1中
        JSONArray jjsxArr = checkReimbursemengInfo.getJSONArray("dt1");
        List<WorkflowRequestTableRecord> detailTableRecord = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(jjsxArr)){
            for (int i = 0; i <jjsxArr.size(); i++) {
                JSONObject  o = jjsxArr.getJSONObject(i);
                List<WorkflowRequestTableField> detailTableField = new ArrayList<>();
                WorkflowRequestTableRecord detailRecord = new WorkflowRequestTableRecord();
                WorkflowRequestTableField jjsx = new WorkflowRequestTableField();
                jjsx.setFieldValue(o.getString("jjsx"));
                jjsx.setFieldName("jjsx");
                jjsx.setView(true);
                jjsx.setEdit(true);
                detailTableField.add(jjsx);
                detailRecord.setWorkflowRequestTableFields(detailTableField.toArray(new WorkflowRequestTableField[detailTableField.size()]));
                detailTableRecord.add(detailRecord);
            }
        }
        WorkflowDetailTableInfo detailTableInfo = new WorkflowDetailTableInfo();
        detailTableInfo.setWorkflowRequestTableRecords(detailTableRecord.toArray(new WorkflowRequestTableRecord[detailTableRecord.size()]));
        //设置到第三个明细表里面
        WorkflowDetailTableInfo[] workflowDetailTableInfos = new WorkflowDetailTableInfo[3];
        workflowDetailTableInfos[2] = detailTableInfo;
        //添加明细表数据
        requestInfo.setWorkflowDetailTableInfos(workflowDetailTableInfos);



        WorkflowBaseInfo baseInfo = new WorkflowBaseInfo();
        baseInfo.setWorkflowId(WorkflowUtil.MARKET_PERSONAL_TAX_ID+"");
        requestInfo.setWorkflowBaseInfo(baseInfo);
        //创建代办为不提交
        requestInfo.setIsnextflow("0");
        requestInfo.setRequestLevel("0");
        requestInfo.setRequestName("营销个税计提单");

        String requestid = workflowService.doCreateWorkflowRequest(requestInfo,user.getUID());
        return requestid;
    }

    @Override
    public List<JSONObject> queryMarketPersonalTaxWorkflowBySqm(List<String> sqm) {
        StringBuilder  querySql = new StringBuilder();
        querySql.append("select a.id,a.requestid,a.yxpthdsqm,wr.currentnodetype from ").append(FormUtil.MARKET_PERSONAL_TAX)
                .append(" a join workflow_requestbase wr on wr.requestid = a.requestid ")
                .append("where a.yxpthdsqm in (");
        sqm.forEach(o->{
            querySql.append("'").append(o).append("'").append(",");
        });
        querySql.deleteCharAt(querySql.length()-1).append(")");
        RecordSet rs = new RecordSet();
        try {
            rs.execute(querySql.toString());
            String[] columns = rs.getColumnName();
            List<JSONObject> results = new ArrayList<>();
            while (rs.next()){
                JSONObject result = new JSONObject();
                for (String column : columns) {
                    result.put(column,rs.getString(column));
                }
                results.add(result);
            }
            return results;
        }catch (Exception e){
            log.error("查询营销个税计提单失败"+e.getMessage());
        }
        return null;
    }
}
